<?php
if(isset($_GET['sc'])){
	session_start();
	session_unset();
	unset($_SESSION['userinfor']);
	setcookie("urlreferopen", '', time()-1000);
	$_ENV['domainroot'] = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.')+1);
	setcookie("urlreferopen", urlencode($_SERVER["HTTP_HOST"]), time()+60*60*24*30, '', $_ENV['domainroot']);
	header('location: http://www.'.$_ENV['domainroot'].'/connectAcountAPIs.php?mod=' . $_GET['mod']);
	exit();
}

define('APPTYPEID', 0);
global $_G;
require './source/class/class_core.php';
$discuz = &discuz_core::instance();

$modarray = array('google','facebook','yahoo');

$mod = !in_array($discuz->var['mod'], $modarray) ? 'google' : $discuz->var['mod'];
define('CURMODULE', $mod);

$cachelist = array('plugin');
$discuz->cachelist = $cachelist;
$discuz->init();
runhooks();
/*
$seccodecheck = 1;
$secqaacheck = 0;
*/
$actarray = array('logging','register','login_success','confirm', 'update');
$act = !in_array($_G['gp_act'], $actarray) ? $actarray[0] : $_G['gp_act'];

if($_G['uid']) {
	$act = 'login_success';
}else{	
	session_start();
	$message = $erro_mes = '';
	$ready_user = $ucresult = array();
	require './OpenIdConnection/function_extra/OpenIdConnection.php';
	$cf = $_G['cache']['plugin']['OpenIdConnection'];
	$userinfor = array();
	
	if(!$_SESSION['userinfor']){
		session_register("userinfor");
		session_register("run_file_start");
		$_SESSION['run_file_start'] = TRUE;
		require './OpenIdConnection/'.$mod.'/start_run.php';
		$userinfor = $_SESSION['userinfor'];
	}else{
		$userinfor = $_SESSION['userinfor'];
	}
	
	if(isset($_POST["register"])) {
		on_register($_POST['username'],$_POST['password'],$_POST['password2'],$userinfor ,$erro_mes);
	}elseif(isset($_POST["confirm"]) || isset($_POST["update"])) {
		on_confirm($_POST['username'],$_POST['password'],$userinfor, $ready_user);
	}else{
		on_checkready($userinfor, $act, $ready_user);
	}
	/*
	echo "<pre>";
	print_r($userinfor);
	echo "<pre>";
	*/
	$opid=DB::fetch_first("SELECT opid FROM ".DB::table('common_openidconnection')." WHERE `uid`='$_G[uid]' AND `key` ='".$mod."'");
}

if($act == 'login_success' && $_COOKIE["urlreferopen"] != $_SERVER['HTTP_HOST'] && $_COOKIE["urlreferopen"]){
	$urlreferend = $_COOKIE["urlreferopen"];
	setcookie("urlreferopen", '', time()-1000);
	if ($_SESSION['run_file_start']){
		unset($_SESSION['userinfor']);
		switch ($mod){
			case 'google':
				unset($_SESSION['token']);
	  			$client->revokeToken();
	  			break;
			case 'yahoo':
				YahooSession::clearSession();
				break;
			default:
				unset($_SESSION['userinfor']);
		}
		session_unset();
	}
	header('location: http://'. urldecode($urlreferend).'/connectAcountAPIs.php?mod=' . $_GET['mod'] . '&act=' . $act);
	exit();
}

if ($userinfor['birthday'] && $act == 'register'){
	list($userinfor_birtday['bmonth'], $userinfor_birtday['bday'], $userinfor_birtday['byear']) = explode('/', $userinfor['birthday']);
}
if($userinfor['gender'] && $act == 'register'){
	$userinfor['gender']  = (strtolower($userinfor['gender']) == 'm' || strtolower($userinfor['gender']) == 'male') ? 'm' : 'f';
}
include template('OpenIdConnection:template');
?>